package org.wanho.shop.dao.impl;

import org.wanho.jdbc.JdbcTemplate;
import org.wanho.shop.dao.UserDao;
import org.wanho.shop.entity.User;
import org.wanho.shop.mapper.UserMapper;
import org.wanho.util.RowMapper;

/**
 * Author：汤小洋
 * Date：2023-04-26 9:39
 * Description：<描述>
 */
public class UserDaoImpl implements UserDao {

    private JdbcTemplate<User> jt = new JdbcTemplate<>();
    private RowMapper<User> rm = new UserMapper();

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   id,username,password,phone,address ")
                .append(" from ")
                .append("   t_user ")
                .append(" where ")
                .append("   username=? and password=? ")
                .toString();
        return jt.queryForObject(sql,rm,username,password);
    }

    @Override
    public User selectByUsername(String username) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   id,username,password,phone,address ")
                .append(" from ")
                .append("   t_user ")
                .append(" where ")
                .append("   username=? ")
                .toString();
        return jt.queryForObject(sql,rm,username);
    }

    @Override
    public void insert(User user) {
        String sql = new StringBuffer()
                .append(" insert into t_user ")
                .append("   (username,password,phone,address) ")
                .append(" values ")
                .append("   (?,?,?,?) ")
                .toString();
        jt.update(sql,user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress());
    }
}
